package com.ruoyi.framework.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constdef.ErrorDef;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.CdUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ShiroFilterUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.json.JsonResult;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author Dale Mao    ---多多 Email:453918229@qq.com
 * @description ClientApiInterceptor
 * @date 2021-01-27 13:37
 */
@Component
public class ClientApiInterceptor extends HandlerInterceptorAdapter {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {


        LoginUser loginUser = (LoginUser) SecurityUtils.getAuthentication().getPrincipal();

        CdUser cdUser=loginUser.getCdUser();
        if(StringUtils.isNotNull(cdUser)){

            return true;
        }
        JsonResult jsonResult=new JsonResult(ErrorDef.USER_ISFORBID, "当前用户被禁止登陆");
        ShiroFilterUtils.out(response, JSON.toJSONString(jsonResult));
        return false;
    }
}
